<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\model\Banner;
use app\common\model\OpusDetails as OpusDetailsModel;
use app\common\service\Album;
use app\common\service\Form;
use app\common\service\Link;
use app\common\service\Opt;
use app\common\service\OpusCategory;
use app\common\service\OpusDetails;
use app\common\service\Team;
use think\Db;
use think\Loader;

class Index extends Frontend
{

    protected $layout = 'default';
    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $recommend=[];


    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        $opusCateGrory=new OpusCategory();
        $opusDetail=new OpusDetails();
        $link=new Link();
        $Album=new Album();
        $banner=new Banner();

        $data['CateGrory']=collection($opusCateGrory->getList(1,1,100))->toArray();

        $data['album']=collection($Album->getList(1,1,100))->toArray();
        $bannerWhere['status']=array('eq',1);
        $data['banner']=$banner->order('weigh desc')->find();

        $whereDetail['classic']=array('eq',1);
        $whereDetail['status']=array('eq',1);
        $data['classic']=collection($opusDetail->getList($whereDetail,1,100))->toArray();

        $whereLink['status']=array('eq',1);
        $data['Link']=collection($link->getList($whereLink,1,8))->toArray();
        $data['recommend']=$this->recommend;


        $this->view->assign($data);


        return $this->view->fetch();
    }

    /**
     * 作品展示
     * @return string
     * @throws \think\Exception
     */
    public function category(){
        $opusCateGrory=new OpusCategory();
        $opusDetail=new OpusDetailsModel();



        $page=$this->request->request('page',1);
        $data['CateGrory']=collection($opusCateGrory->getList(1,1,100))->toArray();

        $data['cid'] = input('cid/d',$data['CateGrory'][0]['id']);

        $data['recommend']=$this->recommend;

        $whereDetail['status']=array('eq',1);
        $whereDetail['category_id']=$data['cid'];

        $data['detail']=$opusDetail->where($whereDetail)->paginate(15);
        $page = $data['detail']->render();



        $this->view->assign($data);
        $this->view->assign('cid',$data['cid']);

        $this->view->assign('page', $page);

        return $this->view->fetch();
    }

    /**
     * 关于我们
     * @return string
     * @throws \think\Exception
     */
    public function company(){
        $opusCateGrory=new OpusCategory();
        $team=new Team();
        if($this->request->isAjax()){

            $data['CateGrory']=collection($opusCateGrory->getList(1,1,100))->toArray();
            $data['team']=collection($team->getList(1,1,100))->toArray();
            $data['recommend']=$this->recommend;
            $this->success('请求成功','',$data);

        }
        return $this->view->fetch();
    }


    public function about()
    {
        $banner=new Banner();
        $data['banner']=$banner->order('weigh desc')->find();

        $list = Db::name('team')->order('weigh desc')->limit(8)->select();



        $this->view->assign($data);
        $this->view->assign('team_list',$list);

        return $this->view->fetch();
    }

    /**
     * 在线下单
     * @return string
     * @throws \think\Exception
     */
    public function overbooking(){
        $opt=new Opt();
        $form=new Form();
        if($this->request->isPost()){
            $request=$this->request->post('name');

            $data=[];
            if($request=='select'){
                $nameWhere['type']=array('eq',1);
                $data['name']=collection($opt->getList($nameWhere,1,100))->toArray();
                $hourWhere['type']=array('eq',2);
                $data['hour']=collection($opt->getList($hourWhere,1,100))->toArray();
                $equipmentWhere['type']=array('eq',3);
                $data['equipment']=collection($opt->getList($equipmentWhere,1,100))->toArray();
                $performerWhere['type']=array('eq',4);
                $data['performer']=collection($opt->getList($performerWhere,1,100))->toArray();
                $directorWhere['type']=array('eq',5);
                $data['director']=collection($opt->getList($directorWhere,1,100))->toArray();
                $dubbingWhere['type']=array('eq',6);
                $data['dubbing']=collection($opt->getList($dubbingWhere,1,100))->toArray();

                $data['recommend']=$this->recommend;
            }elseif ($request=='add'){
                if(!$this->request->post('liaison')){
                    $this->error('请输入联系人');
                }
                if(!$this->request->post('iphone')){
                    $this->error('请输入手机号');
                }
                $check = '/^(1(([35789][0-9])|(47)))\d{8}$/';
                if (!preg_match($check, $this->request->post('iphone'))) {
                    $this->error('请输入正确的手机号');
                }
//                if(!$this->request->post('remarks') || mb_strlen($this->request->post('remarks')) > 50 ){
//                    $this->error('请输入50字以内的备注信息');
//                }

                $validate =  Loader::validate('Form');
                $data['film_class']=$this->request->post('film_class');
                $data['duration']=$this->request->post('duration');
                $data['equipment']=$this->request->post('equipment');
                $data['performer']=$this->request->post('performer');
                $data['team']=$this->request->post('team');
                $data['dubbing']=$this->request->post('dubbing');
                $data['remarks']=$this->request->post('remarks');
                $data['liaison']=$this->request->post('liaison');
                $data['iphone']=$this->request->post('iphone');
                $data['createtime']=time();
                if($validate->check($data)){
                    $data=$form->add($data);
                }else{
                    $this->error($validate->getError());
                }
            }else{
                $this->error('类型错误');
            }
            
            $this->success('请求成功','',$data);

        }

        return $this->view->fetch();
    }






}
